<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Sanitization</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="filter.examples.validation.html">Validation</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="ref.filter.html">Filter 函数</a></div>
 <div class="up"><a href="filter.examples.html">范例</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="filter.examples.sanitization" class="section">
  <h2 class="title">Sanitization</h2>
  <p class="para">
   <div class="example" id="example-4991">
    <p><strong>Example #1 Sanitizing and validating email addresses</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$a&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'joe@example.org'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$b&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'bogus&nbsp;-&nbsp;at&nbsp;-&nbsp;example&nbsp;dot&nbsp;org'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$c&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'(bogus@example.org)'</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$sanitized_a&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">filter_var</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">FILTER_SANITIZE_EMAIL</span><span style="color: #007700">);<br />if&nbsp;(</span><span style="color: #0000BB">filter_var</span><span style="color: #007700">(</span><span style="color: #0000BB">$sanitized_a</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">FILTER_VALIDATE_EMAIL</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"This&nbsp;(a)&nbsp;sanitized&nbsp;email&nbsp;address&nbsp;is&nbsp;considered&nbsp;valid.\n"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">$sanitized_b&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">filter_var</span><span style="color: #007700">(</span><span style="color: #0000BB">$b</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">FILTER_SANITIZE_EMAIL</span><span style="color: #007700">);<br />if&nbsp;(</span><span style="color: #0000BB">filter_var</span><span style="color: #007700">(</span><span style="color: #0000BB">$sanitized_b</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">FILTER_VALIDATE_EMAIL</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"This&nbsp;sanitized&nbsp;email&nbsp;address&nbsp;is&nbsp;considered&nbsp;valid."</span><span style="color: #007700">;<br />}&nbsp;else&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"This&nbsp;(b)&nbsp;sanitized&nbsp;email&nbsp;address&nbsp;is&nbsp;considered&nbsp;invalid.\n"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">$sanitized_c&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">filter_var</span><span style="color: #007700">(</span><span style="color: #0000BB">$c</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">FILTER_SANITIZE_EMAIL</span><span style="color: #007700">);<br />if&nbsp;(</span><span style="color: #0000BB">filter_var</span><span style="color: #007700">(</span><span style="color: #0000BB">$sanitized_c</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">FILTER_VALIDATE_EMAIL</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"This&nbsp;(c)&nbsp;sanitized&nbsp;email&nbsp;address&nbsp;is&nbsp;considered&nbsp;valid.\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Before:&nbsp;</span><span style="color: #0000BB">$c</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"After:&nbsp;&nbsp;</span><span style="color: #0000BB">$sanitized_c</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;&nbsp;&nbsp;&nbsp;&nbsp;<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
This (a) sanitized email address is considered valid.
This (b) sanitized email address is considered invalid.
This (c) sanitized email address is considered valid.
Before: (bogus@example.org)
After: bogus@example.org
</pre></div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-4992">
    <p><strong>Example #2 Configuring a default filter</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
filter.default&nbsp;=&nbsp;full_special_chars<br />filter.default_flags&nbsp;=&nbsp;0</span>
</code></div>
    </div>

   </div>
  </p>
 </div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="filter.examples.validation.html">Validation</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="ref.filter.html">Filter 函数</a></div>
 <div class="up"><a href="filter.examples.html">范例</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
